package com.wifipositioner.activity;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.wifipositioner.Globals;
import com.wifipositioner.R;

public class SettingsActivity extends ListActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		ArrayList<HashMap<String,String>> data = new ArrayList<HashMap<String,String>>();
		HashMap<String,String> settingsMap = new HashMap<String, String>();
		settingsMap.put("Key", Globals.s_weight90);
		settingsMap.put("Value", Globals.weight90 + "");
		data.add(settingsMap);
		settingsMap = new HashMap<String, String>();
		settingsMap.put("Key", Globals.s_weight80);
		settingsMap.put("Value", Globals.weight80 + "");
		data.add(settingsMap);
		settingsMap = new HashMap<String, String>();
		settingsMap.put("Key", Globals.s_weight70);
		settingsMap.put("Value", Globals.weight70 + "");
		data.add(settingsMap);
		settingsMap = new HashMap<String, String>();
		settingsMap.put("Key", Globals.s_weight60);
		settingsMap.put("Value", Globals.weight60 + "");
		data.add(settingsMap);
		settingsMap = new HashMap<String, String>();
		settingsMap.put("Key", Globals.s_maxMeasureCntr);
		settingsMap.put("Value", Globals.maxMeasureCntr + "");
		data.add(settingsMap);
		
		int[] array = {R.id.settingskey, R.id.settingsvalue};
		SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.settingslistitem, new String[]{"Key", "Value"}, array);


		setListAdapter(adapter);
		super.onCreate(savedInstanceState);
	}
	@Override
	protected void onListItemClick(ListView l, final View itemView, int position, long id) {
         LayoutInflater inflater = (LayoutInflater) this
                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         View layout = inflater.inflate(R.layout.filenamepopup,
                 (ViewGroup) findViewById(R.id.filenamepopup_root));
         final PopupWindow popupWindow = new PopupWindow(layout, 300, 600, true);
         popupWindow.showAtLocation(layout, Gravity.CENTER, 0, 0);
         
         Button btn_cancel = (Button)popupWindow.getContentView().findViewById(R.id.filenamepopup_btn_cancel);
         Button btn_ok = (Button)popupWindow.getContentView().findViewById(R.id.filenamepopup_btn_ok);
         final EditText et_input = (EditText)popupWindow.getContentView().findViewById(R.id.filenamepopup_edittext_filename);
         
         btn_cancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				popupWindow.dismiss();
				
			}
		});
        btn_ok.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				((TextView)itemView.findViewById(R.id.settingsvalue)).setText(et_input.getText().toString());
				setGlobalValue(((TextView)itemView.findViewById(R.id.settingskey)).getText().toString(),
								et_input.getText().toString());
				popupWindow.dismiss();
			}
		});
        super.onListItemClick(l, itemView, position, id);
	}
	private void setGlobalValue(String key, String value) {
		if(key.equals(Globals.s_weight60)){
			Globals.weight60 = Double.parseDouble(value);
		}
		else if(key.equals(Globals.s_weight70)){
			Globals.weight70 = Double.parseDouble(value);
		}
		else if(key.equals(Globals.s_weight80)){
			Globals.weight80 = Double.parseDouble(value);
		}
		else if(key.equals(Globals.s_weight90)){
			Globals.weight90 = Double.parseDouble(value);
		}
		else if(key.equals(Globals.s_maxMeasureCntr)){
			Globals.maxMeasureCntr = Integer.parseInt(value);
		}
	}
}
